package net.shouyong.topbilliards.fragment;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.CountCallback;
import com.avos.avoscloud.SaveCallback;

import net.shouyong.topbilliards.R;
import net.shouyong.topbilliards.bean.BilliardRecord;
import net.shouyong.topbilliards.fragment.HomeFragment.OnListFragmentInteractionListener;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link BilliardRecord} and makes a call to the
 * specified {@link OnListFragmentInteractionListener}.
 * TODO: Replace the implementation with code for your data type.
 */
public class BillirdrecordRecyclerViewAdapter extends RecyclerView.Adapter<BillirdrecordRecyclerViewAdapter.ViewHolder> {

    private final List<? extends  BilliardRecord> mValues;
    private final Action mListener;

    public BillirdrecordRecyclerViewAdapter(List<? extends  BilliardRecord> items, Action listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_billirdrecord_item1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.update();
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public final TextView itemCost;
        public final TextView endTime;
        public final TextView notice;
        public final Button mButton;
        public BilliardRecord mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mContentView = (TextView) view.findViewById(R.id.start_time);
            endTime = (TextView) view.findViewById(R.id.end_time);
            itemCost = (TextView) view.findViewById(R.id.item_cost);
            notice = (TextView) view.findViewById(R.id.notice);
            mButton = (Button)view.findViewById(R.id.option);
        }

        public void update(){
            mIdView.setText(String.valueOf(mItem.getIndex()));

//            String cost = String.valueOf(mItem.getCost());
            notice.setText(String.format(notice.getContext().getString(R.string.notice),mItem.getIndex(), mItem.getUnitPrice()));
            if (mItem.isStart()) {
                mContentView.setText(String.format(mContentView.getContext().getString(R.string.starttime_m), mItem.getStartTime()));
                //endTime.setText(String.format(endTime.getContext().getString(R.string.starttime_m), mItem.getEndTime()));
                endTime.setText(mContentView.getContext().getString(R.string.costing));
                itemCost.setText(String.format(itemCost.getContext().getString(R.string.item_cost), mItem.getCost()));
            }else {
                mContentView.setText(mContentView.getContext().getString(R.string.not_start));
                endTime.setText("");
                itemCost.setText("");
            }

            if (mItem.isStart()) {
                mButton.setText("结束");
            } else {
                mButton.setText("开始");
            }
            mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (!mItem.isStart()){
                        new AlertDialog.Builder(v.getContext())
                                .setTitle("提示")
                                .setMessage("确定对"+mItem.getIndex()+"号桌计费？")
                                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mItem.setStartTime(new Date());
                                        ((Button)v).setText("结束");
                                        mListener.start(ViewHolder.this);
                                    }
                                })
                                .setNegativeButton("否", null)
                                .show();
                    }else {
                        new AlertDialog.Builder(v.getContext())
                                .setTitle("提示")
                                .setMessage("确定对"+mItem.getIndex()+"号桌收费"+mItem.getCost()+" 元。")
                                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mItem.setEndTime(new Date());
                                        mItem.setSettlement(mItem.getCost());
                                        mListener.stop(ViewHolder.this);
                                    }
                                })
                                .setNegativeButton("否", null)
                                .show();
                    }
                }
            });
        }


        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }

    public interface Action
    {
        void start(ViewHolder viewHolder);
        void stop(ViewHolder viewHolder);
    }
}
